package umls.expander;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;

import jdbc.UMLS_DB_Interface;

public class Keyword {

	public String GetKeywordBySingleBiologicalProcess(String BiologicaProcess,
			UMLS_DB_Interface umlsdb) throws SQLException {
		StringBuilder result = new StringBuilder();

		result.append(PrintUniqueRelatedTerms(BiologicaProcess, umlsdb));

		return result.toString();
	}

	public String GetKeywordBySingleGene(String Gene, UMLS_DB_Interface umlsdb)
			throws SQLException {
		StringBuilder result = new StringBuilder();

		result.append(PrintUniqueRelatedTerms(Gene, umlsdb));

		return result.toString();
	}

	public String GetKeywordBySingleBiologicalProcessFilteredBySemType(
			String BiologicaProcess, UMLS_DB_Interface umlsdb,
			SemGroupConfReader semgroupconfreader) throws SQLException {
		StringBuilder result = new StringBuilder();

		result.append(PrintUniqueRelatedTermsFilteredBySemType(
				BiologicaProcess, umlsdb, semgroupconfreader));

		return result.toString();
	}

	public String GetKeywordBySingleGeneFilteredBySemType(String Gene,
			UMLS_DB_Interface umlsdb, SemGroupConfReader semgroupconfreader)
			throws SQLException {
		StringBuilder result = new StringBuilder();

		result.append(PrintUniqueRelatedTermsFilteredBySemType(Gene, umlsdb,
				semgroupconfreader));

		return result.toString();
	}

	private static String PrintUniqueRelatedTerms(String termstring,
			UMLS_DB_Interface umlsdb) throws SQLException {
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");

		Term genericterm = new Term(termstring);

		genericterm.CreateConcepts(umlsdb);

		HashSet<String> uniquerelatedterms = genericterm
				.GetUniqueCompleteRelatedTermList();

		Iterator<String> uniquerelatedterms_itr = uniquerelatedterms.iterator();

		while (uniquerelatedterms_itr.hasNext()) {
			result.append(uniquerelatedterms_itr.next() + NEW_LINE);
		}

		return result.toString();
	}

	private static String PrintUniqueRelatedTermsFilteredBySemType(
			String termstring, UMLS_DB_Interface umlsdb,
			SemGroupConfReader semgroupconfreader) throws SQLException {
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");

		Term genericterm = new Term(termstring);

		genericterm.CreateConcepts(umlsdb);

		HashSet<String> uniquerelatedterms = genericterm
				.GetUniqueCompleteRelatedTermListFilteredBySemType(semgroupconfreader);

		Iterator<String> uniquerelatedterms_itr = uniquerelatedterms.iterator();

		while (uniquerelatedterms_itr.hasNext()) {
			result.append(uniquerelatedterms_itr.next() + NEW_LINE);
		}

		return result.toString();
	}
}
